package com.superhelper.receiver.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.superhelper.common.domain.ele.ElePushEntity;
import com.superhelper.common.service.AliMQProducerService;
import com.superhelper.receiver.service.ElePushValidService;

/**
 * 饿了么消息推送
 */
@Controller
@RequestMapping("/push/ele")
public class EleReceiverController {
    protected Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AliMQProducerService producer;

    @Autowired
    private ElePushValidService validService;

    @RequestMapping(value = { "", "/" }, method = RequestMethod.GET)
    @ResponseBody
    public String recieveEmpty(HttpServletRequest request) {
        return "{\"message\":\"ok\"}";
    }

    @RequestMapping(value = { "", "/" }, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> recieve(@RequestBody ElePushEntity req, HttpServletResponse response) {
        Map<String, String> result = new HashMap<>();
        log.info("[/push/ele] params:{}", req);
        if (!validService.valid(req)) {
            log.info("[/push/ele] sign error requestId:{}", req.getRequestId());
            // result.put("message", "fail");
            // return result;
        }
        try {
            producer.sendEleOrder(req);
        } catch (Exception e) {
            log.error("[/push/ele] deal error requestId:{}", req.getRequestId(), e);
            result.put("message", "fail");
        }
        result.put("message", "ok");
        return result;
    }

}
